package ThreadDemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-10-12
 * Time: 11:18
 */
public class Demo11 {
    //线程终止相关操作
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("hello,Thread");
                try {
                    Thread.sleep(1000);//sleep()被提前唤醒，会抛出一个异常，并且还会把将isInterrupted()重新置为false;
                } catch (InterruptedException e) {

                }
            }
        },"Thread_1");

        thread.start();

        Thread.sleep(3000);
        thread.interrupt();//标志位置为true,同时会提前唤醒sleep()方法,并抛出异常
    }
}
